﻿using System;
using System.Collections.Generic;
using System.Helpers;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DataProcessModule.Protocol
{
    public class FireboxSoftwareUpgrade : BaseInstruction
    {
        #region param

        private string UpgradeVersion;//升级版本号	4	DWORD

        private string UpgradeFireboxID;//待升级防火盒ID	4	BYTE

        private string FTPServerAddress; //FTP服务器IP	4	BYTE

        private string FilePath; //文件路径	32	CHAR

        private string FileName;//文件名	16	CHAR

        private string MD5CheckCode;//MD5值	16	BYTE

        #endregion

        #region attributes

        public string AtUpgradeVersion { 
            get { return UpgradeVersion; }
            set { UpgradeVersion = value; }
        }

        public string AtUpgradeFireboxID { 
            get { return UpgradeFireboxID; }
            set { UpgradeFireboxID = value; }
        }

        public string AtFTPServerAddress {
            get { return FTPServerAddress; }
            set { FTPServerAddress = value; }
        }

        public string AtFilePath {
            get { return FilePath; }
            set { FilePath = value; }
        }

        public string AtFileName {
            get { return FileName; }
            set { FileName = value; }
        }

        public string AtMD5CheckCode {
            get { return MD5CheckCode; }
            set { MD5CheckCode = value; }
        }

        #endregion

        #region construction
        public FireboxSoftwareUpgrade()
        {
            instHeadInfo.instID = 0x0116;          
        }

        public FireboxSoftwareUpgrade(byte[] buf)
        {
            instHeadInfo.instID = 0x0116;    
            int offset = 0;
            // 获取头信息
            GetHeadInfo(buf, ref offset);
            BodyStreamSplit(buf, ref offset);
        }

        #endregion

        #region function

        public void BodyStreamSplit(byte[] buf, ref int offset)
        {
            try
            {
                UpgradeVersion = BC.ToBCDString(buf, ref offset,4);
                UpgradeFireboxID = BC.ToBCDString(buf, ref offset, 4);
                int address_a = buf[offset++];
                int address_b = buf[offset++];
                int address_c = buf[offset++];
                int address_d = buf[offset++];
                FTPServerAddress = address_a.ToString() + '.' + address_b.ToString() + '.' + address_c.ToString() + '.' + address_d.ToString();
                FilePath = BC.GetBufferString(buf, offset, 32);
                offset += 32;
                FileName = BC.GetBufferString(buf, offset, 16);
                offset += 16;
                MD5CheckCode = BC.ToBCDString(buf, ref offset, 16);
            }
            catch (Exception e) { }
        }

        public string CombinedSqlString()
        {
            string _sql = string.Empty;
            _sql = "INSERT INTO table_fire_box_software_upgrade (" +
               "TerminalKey,UpgradeVersion,UpgradeFireboxID,FTPServerAddress" +
               ",FilePath,FileName,MD5CheckCode,Upgradable) VALUES('" + instHeadInfo.tdPhoneNumber +
               "','" + UpgradeVersion + "','" + UpgradeFireboxID + "','" + FTPServerAddress + "','"
               + FilePath + "','" + FileName + "','" + MD5CheckCode + "',1) ON DUPLICATE KEY " +
               "UPDATE UpgradeVersion=VALUES(UpgradeVersion),UpgradeFireboxID=VALUES(UpgradeFireboxID)" +
               ",FTPServerAddress=VALUES(FTPServerAddress),FilePath=VALUES(FilePath),FileName=VALUES(FileName)" +
               ",MD5CheckCode=VALUES(MD5CheckCode),Upgradable=1;";
            return _sql;
        }
        public static string AutoUpgradeString(string termkey)
        {
            string _sql = string.Empty;
            _sql = "INSERT IGNORE INTO table_fire_box_software_upgrade (TerminalKey,UpgradeFireboxID," +
            "FTPServerAddress,FileName,Upgradable) VALUES('" + termkey + "','" + termkey + "','180.169.121.50','PROG.BIN',0);";
            return _sql;
        }

        public override byte[] GenerateInstructionStream()
        {
            #region 处理头信息
            // 计算指令体长度，此处赋值给instbodyproperty等同于在1~10bit位置写入长度           
            InstBodyLength = 76;
            #endregion

            #region 处理指令体
            int offset = 0;
            instBodyData = new byte[InstBodyLength];
            try
            {
                //BC.WriteBCD(this.UpgradeVersion, 0, 8, instBodyData, ref offset);              
                UInt32 _upver = Convert.ToUInt32(this.UpgradeVersion);
                BC.WriteNet(_upver, instBodyData, ref offset);
                //BC.WriteBCD(this.UpgradeFireboxID, 0, 8, instBodyData, ref offset);
                BC.WriteStringToByteArrayFastest(this.UpgradeFireboxID, instBodyData, ref offset);
                string[] _sads = FTPServerAddress.Split('.');
                instBodyData[offset++] = Convert.ToByte(_sads[0]);
                instBodyData[offset++] = Convert.ToByte(_sads[1]);
                instBodyData[offset++] = Convert.ToByte(_sads[2]);
                instBodyData[offset++] = Convert.ToByte(_sads[3]);
                //BC.WriteBCD(this.FTPServerAddress, 0, 8, instBodyData, ref offset);
                BC.WriteCharString(this.FilePath, 0, FilePath.Length, instBodyData, offset);
                offset += 32;
                BC.WriteCharString(this.FileName, 0, FileName.Length, instBodyData, offset);
                offset += 16;
                //BC.WriteBCD(this.MD5CheckCode, 0, 32, instBodyData, ref offset);
                BC.WriteStringToByteArrayFastest(this.MD5CheckCode, instBodyData, ref offset);
            }
            catch (Exception e) { }
            #endregion
            return base.GenerateInstructionStream();
        }
        #endregion
    }
}
